<?php
/**
 * Created by PhpStorm.
 * User: User
 * Date: 8/2/14
 * Time: 10:01 AM
 */
namespace Phalconvn\Shop;
use Phalcon\Mvc\User\Component;
use Apps\Backend\Models\SellsHot;
use Apps\Backend\Models\SellsCombo;
use Apps\Backend\Models\SellsOnce;
use Apps\Backend\Models\Products;
class Cart extends Component {
    public function buy($data){
        $sell = $this->sellPrice($data);
        $ss = $this->session->get('frontend_shopping_cart');
        if(empty($ss)){
            $priceOrder = $sell > 10 ?  $sell : $data->price;
            $cart[$data->id] = array('name'=>$data->name,'price'=>$priceOrder,'image'=>PUBLIC_FILE.'products/img100x100/'.$data->picture,
                          'linkHref'=>PUBLIC_URL.$data->alias.'.html','colorName'=>$data->color,
                          'featuress'=> $this->featuress($data->featuress,$data->category_id),
                          'code'=>$data->sku,'quantity'=>1);


            $this->session->set('frontend_shopping_cart',$cart);
            $this->sellCombo($data->id);
            $this->sellOnce($data->id);
        }else{
            $cart = $ss;
            if(array_key_exists($data->id,$cart)){
                if($cart[$data->id]['quantity'] <5){
                    $quantity = $cart[$data->id]['quantity'] + 1;
                    $cart[$data->id]['quantity'] = $quantity ;
                }
            }else{
                $priceOrder = $sell > 10?$sell:$data->price;
                $cart[$data->id] = array('name'=>$data->name,'price'=>$priceOrder,'image'=>PUBLIC_FILE.'products/img100x100/'.$data->picture,
                    'linkHref'=>PUBLIC_URL.$data->alias.'.html','colorName'=>$data->color,
                    'featuress'=> $this->featuress($data->featuress,$data->category_id),
                    'code'=>$data->sku,'quantity'=>1);

            }
           $this->session->set('frontend_shopping_cart',$cart);
           $this->sellCombo($data->id);
           $this->sellOnce($data->id);
        }

    }
    public function sellPrice($data){
        $sell =0;
        $resultProduct   = $this->viewCache->get('sells_hot_frontend_hot');
        if($resultProduct != null){
            if($resultProduct['product']==$data->id)
                $sell = $resultProduct['sell'];
            else
                $sell = $data->sell;
        }
        return $sell;
    }
    private  function sellCombo($id){
        $cart = $this->session->get('frontend_shopping_cart');
        $date = (int)strtotime(date("Y-m-d H:i:s"));
        $combo = SellsCombo::findFirst(array("product_one = $id  OR product_two = $id AND status = 1 AND begin_day <= $date AND end_day > $date"));
        if($combo != '' ){
            if($combo->product_one == $id){
                $idNew = $combo->product_two;
            }else{
                $idNew = $combo->product_one;
            }
            $tbCombo = Products::findFirst(array("id = $idNew AND level = 2",'columns'=>'id,name,alias,price,hot,deal,sell,picture,code,featuress,category_id,color,sku'));
            $sell = $this->sellPrice($tbCombo);
            $cart[$idNew] = array('name'=>$tbCombo->name,'price'=>$sell,'image'=>PUBLIC_FILE.'products/img100x100/'.$tbCombo->picture,
                'linkHref'=>PUBLIC_URL.$tbCombo->alias.'.html','colorName'=>$tbCombo->color,
                'featuress'=> $this->featuress($tbCombo->featuress,$tbCombo->category_id),
                'code'=>$tbCombo->sku,'quantity'=>1);
            $key = $combo->product_one.'-'.$combo->product_two;
            $this->session->set('frontend_shopping_cart_combo',array($key=>array('name'=>$combo->name,'price'=>$combo->price,'id'=>"$combo->product_one,$combo->product_two")));
            $this->session->set('frontend_shopping_cart',$cart);
        }
    }
    public function featuress($data,$category){
        $resultData = unserialize($data);
        if($category ==17){
            $result ='Màn hình:'.$resultData['manhinh'] .' / Camera: '. $resultData['camchinh'].' / Pin: '.$resultData['dungluongpin'];
        }elseif($category < 38 && $category !=17){
            $result = 'Màn hình:'.$resultData['manhinh'] .' / Camera: '. $resultData['camchinh'].' / Pin: '.$resultData['dungluongpin'];
        }elseif($category > 38  ){
            $result = $resultData['chatlieu']!=''?'Chất liệu: '.$resultData['chatlieu'].' / ':'' .
                      $resultData['ketnoipk']!=''?'Chuẩn kết nối: '.$resultData['ketnoipk'].' / ':'' .
                      $resultData['dungluongpk']!=''?'Dung lượng: '.$resultData['dungluongpk'].' / ':'' .
                      $resultData['congsuatpk']!=''?'Công suất: '.$resultData['congsuatpk'].' / ':'' .
                      $resultData['tinhnangpk']!=''?'Tính năng khác: '.$resultData['tinhnangpk']:'';
        }
        return $result;
    }
    public function removeItem($id){
        $ss = $this->session->get('frontend_shopping_cart');
        if(isset($ss[$id])){
           /* $ssCombo = $this->session->get('frontend_shopping_cart_combo');
            $combo   = (int)strstr($ssCombo['id'], $id, true);
            if($combo > 0){
                $this->session->set('frontend_shopping_cart_combo',array('price'=>0,'id'=>0));
            }*/
            if($this->checkOnce($id)==false){
                unset($ss[$id]);
                $this->session->set('frontend_shopping_cart',$ss);
                $this->removeCombo($id);
                $this->removeOnce($id);
            }
        }
    }
    public function changeItem($id,$number){
        $cart = $this->session->get('frontend_shopping_cart');
        if(array_key_exists($id,$cart)){
            if($number <6){
                $cart[$id]['quantity'] = $number ;
            }else{
                $cart[$id]['quantity'] = 5 ;
            }
        }
       
        $this->session->set('frontend_shopping_cart',$cart);
        $this->checkCombo($id);
        $this->changeOnce($id);
    }
    
    private function checkCombo($id){
        $date = (int)strtotime(date("Y-m-d H:i:s"));
        $combo = SellsCombo::findFirst(array("product_one = $id  OR product_two = $id AND status = 1 AND begin_day <= $date AND end_day > $date"));
        if(!empty($combo)){
            $ss = $this->session->get('frontend_shopping_cart');
            if( $ss[$combo->product_one]['quantity'] >0 && $ss[$combo->product_two]['quantity'] >0){
                $sscombo = $this->session->get('frontend_shopping_cart_combo');
                $key = $combo->product_one.'-'.$combo->product_two;
                
                $quantity1 = $ss[$combo->product_one]['quantity'];
                $quantity2 = $ss[$combo->product_two]['quantity'];
                
                $quantity = $quantity1>$quantity2?$quantity2:$quantity1;
                $sscombo[$key]['price'] =$combo->price*$quantity;    
                $sscombo = $this->session->set('frontend_shopping_cart_combo',$sscombo);
            }
            
             
        }
    }
    
    private function removeCombo($id){
        $date = (int)strtotime(date("Y-m-d H:i:s"));
        $combo = SellsCombo::findFirst(array("product_one = $id  OR product_two = $id AND status = 1 AND begin_day <= $date AND end_day > $date"));
        if(!empty($combo)){
            $ss = $this->session->get('frontend_shopping_cart');
            $sscombo = $this->session->get('frontend_shopping_cart_combo');
            $key = $combo->product_one.'-'.$combo->product_two;
            
            unset($sscombo[$key]);
            $sscombo = $this->session->set('frontend_shopping_cart_combo',$sscombo);
            //print_r($sscombo);
        }
    }
    
    private function sellOnce($id){
        $cart = $this->session->get('frontend_shopping_cart');
        $date = (int)strtotime(date("Y-m-d H:i:s"));
        $sellonce = SellsOnce::findFirst(array("product_one = $id AND status = 1 AND begin_day <= $date AND end_day > $date"));
        if(!empty($sellonce)){
            if($sellonce->product_one == $id){
                $idNew = $sellonce->product_two;
            }
            $tbOnce = Products::findFirst(array("id = $idNew",'columns'=>'id,name,alias,price,hot,deal,sell,picture,code,featuress,category_id,color,sku'));
            $sell = $this->sellPrice($tbOnce);
            $priceOrder = $sell > 10 ?  $sell : $tbOnce->price;
            $cart[$idNew] = array('name'=>$tbOnce->name,'price'=>$priceOrder,'image'=>PUBLIC_FILE.'products/img100x100/'.$tbOnce->picture,
                'linkHref'=>PUBLIC_URL.$tbOnce->alias.'.html','colorName'=>$tbOnce->color,
                'featuress'=> $this->featuress($tbOnce->featuress,$tbOnce->category_id),
                'code'=>$tbOnce->sku,'quantity'=>$cart[$id]['quantity']);
            $key = $sellonce->product_one.'-'.$sellonce->product_two;
            $totalprice = $priceOrder*$cart[$id]['quantity'];
            $this->session->set('frontend_shopping_cart_once',array($key=>array('name'=>$sellonce->name,'price'=>$totalprice,'id'=>"$sellonce->product_one,$sellonce->product_two")));
            $this->session->set('frontend_shopping_cart',$cart);
        }
        
    }
    
    private function changeOnce($id){
        $cart = $this->session->get('frontend_shopping_cart');
        $date = (int)strtotime(date("Y-m-d H:i:s"));
        $sellonce = SellsOnce::findFirst(array("product_one = $id AND status = 1 AND begin_day <= $date AND end_day > $date"));
        if(!empty($sellonce)){
            $once = $this->session->get('frontend_shopping_cart_once');
            if($sellonce->product_one == $id){
                $idNew = $sellonce->product_two;
            }
            $tbOnce = Products::findFirst(array("id = $idNew",'columns'=>'id,name,alias,price,hot,deal,sell,picture,code,featuress,category_id,color,sku'));
            $sell = $this->sellPrice($tbOnce);
            $priceOrder = $sell > 10 ?  $sell : $tbOnce->price;
            $cart[$idNew]['quantity'] = $cart[$id]['quantity'];
            $key = $sellonce->product_one.'-'.$sellonce->product_two;
            $once[$key]['price'] = $priceOrder*$cart[$id]['quantity'];
            $this->session->set('frontend_shopping_cart_once',$once);
            $this->session->set('frontend_shopping_cart',$cart);
        }
    }
    
    private function removeOnce($id){
        $cart = $this->session->get('frontend_shopping_cart');
        $date = (int)strtotime(date("Y-m-d H:i:s"));
        $sellonce = SellsOnce::findFirst(array("product_one = $id AND status = 1 AND begin_day <= $date AND end_day > $date"));
        if(!empty($sellonce)){
            $once = $this->session->get('frontend_shopping_cart_once');
            
            $key = $sellonce->product_one.'-'.$sellonce->product_two;
            unset($cart[$sellonce->product_two]);
            unset($once[$key]);
            
            $this->session->set('frontend_shopping_cart_once',$once);
            $this->session->set('frontend_shopping_cart',$cart);
        }
    }
    
    private function checkOnce($id){
        $date = (int)strtotime(date("Y-m-d H:i:s"));
        $sell = SellsOnce::findFirst(array("product_two = $id AND status = 1 AND begin_day <= $date AND end_day > $date"));
        if(!empty($sell)){
            return true;
        }else{
            return false;
        }
    }
    
    

}